import util from "./util.js"
import requestUtil from "./requestUtil.js"
import strUtil from "./strUtil.js"

function Core() {

}



var get_wxh5_User=function(func,param,g_prepage){
			if(!util.isWeixin()){return }
	
			var userInfo = util.getUser();
			if(!!userInfo && !!userInfo.wxOpenId){return }
			
			var url = location.href.split("#")[0];
			var query = util.getQuery();
			if(!query["code"]){
				if(!!param) util.data_local("wxreg_param",param);
				if(!!g_prepage) util.data_local("pre_guider_page",g_prepage);
				
			    requestUtil.post("wxapi/authorize",{"dsturl":encodeURIComponent(url)},function(res){
					location.replace(res.data)	
			    })
			}else{
				var o={};
				o.code=query["code"];				
				let wxreg_param=util.data_local("wxreg_param");
				if(!!wxreg_param){
					for(let i in wxreg_param){
						let item=wxreg_param[i];
						if(!strUtil.isNull(item))o[i]=item;					
					}
					util.data_local("wxreg_param",null);
				}
							
			    requestUtil.post("wxapi/userinfo",o,(resp)=>{ 
					 if(!!func&&typeof func=="function"){func(resp);}	
			    },function(r){})
			}
}

var __getwxuser__=function(pre_guider_page){
	let query=util.getQuery();
	let o={};
	for(var key in query){
		o[key]=query[key];
	}
	
	get_wxh5_User((res)=>{
		if(res.status==200){
			util.toast("getwxuser2 callback")
			util.setUser(res.data);
			let page=util.data_local("pre_guider_page");
			if(!!page){
				util.data_local("pre_guider_page",null);
				if(util.isWeixin()){
					location.replace(page);
				}else{
					util.navigateTo(page);
				}			
			}
		}			
	},o,pre_guider_page)
}
var __getwxuser=function(pre_guider_page){
	let _CONFIG=util.getConfig();
	let val=_CONFIG.wxh5_refresh_stat||'0';
	let local_version=util.data_local("wxh5_refresh_stat")||"0";
	if(local_version!=val){
		util.rmLoginMsg();		
		util.data_local("wxh5_refresh_stat",val)
	}
	__getwxuser__(pre_guider_page);
	
}

var ___updatewxuser=function(pre_guider_page){
	util.rmLoginMsg();
	__getwxuser__(pre_guider_page);
}


/* =================================h5 */




var ____appgetwxuser=function(){
	let _CONFIG=util.getConfig();
	let val=_CONFIG.wxapp_refresh_stat||'0';
	let local_version=util.data_local("wxapp_refresh_stat")||"0";
	if(local_version!=val){
		util.rmLoginMsg();		
		util.data_local("wxapp_refresh_stat",val)
	}
	____appgetwxuser___();
}

var ____appgetwxuser___=function(){
	let user=util.getUser();
	if(!!user&&!!user.wxAppOpenId)return;
	uni.login({
	  provider: 'weixin',
	  success: function (loginRes) {
		  let auth=loginRes.authResult;
		  let o={"token":auth.access_token,"openid":auth.openid};
		  requestUtil.post("wxapi/userinfo_app",o,res=>{		  
			 util.setUser(res.data);
		  },err=>{
			 util.toast(JSON.stringify(err))
		  })
	  }
	});	
}

var ___updateappwxuser=function(pre_guider_page){
	util.rmLoginMsg();
	____appgetwxuser___();
}

/* ===========================app */


var ____mpgetwxuser=function(param){
	
	let _CONFIG=util.getConfig();
	let val=_CONFIG.wxmp_refresh_stat||'0';
	let local_version=util.data_local("wxmp_refresh_stat")||"0";
	if(local_version!=val){
		util.rmLoginMsg();		
		util.data_local("wxmp_refresh_stat",val)
	}
	____mpgetwxuser___(param);
}

var ____mpgetwxuser___=function(param){
	let user=util.getUser();
	if(!!user&&!!user.wxMpOpenId)return;
	uni.login({
	  provider: 'weixin',
	  success: function (loginRes) {
		  let o={"code":loginRes.code};
		  if(!!param){ for(let p_i in param){o[p_i]=param[p_i];}}
		  requestUtil.post("wxapi/userinfo_mp",o,res=>{		  
			 util.setUser(res.data);		 
		  },err=>{
			 util.toast(JSON.stringify(err))
		  })
	  }
	});	
}

var ___updatempwxuser=function(pre_guider_page){
	util.rmLoginMsg();
	____mpgetwxuser___();
}

Core.prototype.wxmpGetUser=function(e){  //小程序按钮点击获取用户信息
	let wx_user=e.detail.userInfo;
	let data=util.getUser();
	data.wxNickName=wx_user.nickName;
	data.wxHeadImg=wx_user.avatarUrl;
	requestUtil.post("user/update",data,res2=>{
		util.setUser(res2.data);
	})	
}
/* ========================================mp */


Core.prototype.wxLogin=function(pre_guider_page,mp_param){
	if(util.isH5()){
		__getwxuser(pre_guider_page);
	}else if(util.isApp()){
		____appgetwxuser();
	}else {
		____mpgetwxuser(mp_param);
	}
}

Core.prototype.updateWxLogin=function(pre_guider_page){
	if(util.isH5()){
		___updatewxuser(pre_guider_page);
	}else if(util.isApp()){
		___updateappwxuser();
	}else {
		___updatempwxuser();
	}
}




var wxUtil = new Core();
export default wxUtil
